package jee.boot.entity.auth;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jee.boot.common.annotation.Table;
import jee.boot.common.annotation.Transient;
import jee.boot.common.basic.DataEntity;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * oauth 认证账户
 */
@Table(name = "at_account")
public class Account extends DataEntity<Account> implements UserDetails {
    private static final long serialVersionUID = 1L;
    /**
     * 登录名(可用于登录)
     */
    private String username;
    /**
     * 电话(可用于登录)
     */
    private String phone;
    /**
     * 邮箱(可用于登录)
     */
    private String email;
    /**
     * 密码
     */
    @Transient(ifDataIsNull = true)
    private String password;
    /**
     * 账户状态
     * account_status_1: 正常;
     * account_status_2: 锁定;
     * account_status_3: 密码过期;
     * account_status_4: 账户过期;
     */
    private String accountStatus;
    /**
     * 是否已重置初始密码Y :是,N:否,字典:yes_no
     */
    private String resetPassword;
    /**
     * 密码错误次数
     */
    private Integer passwordErrors;
    /**
     * 密码失效日期(默认一个月)
     */
    private Date passwordInvalidDate;
    /**
     * 账户失效日期(默认一年)
     */
    private Date accountInvalidDate;
    /**
     * 登录账户的名称,主要用于创建人的显示使用
     */
    private String name;

    //---------------------
    @Transient
    private String oldPassword;//旧密码
    @Transient
    private String newPassword;//新密码

    public Account() {
        super();
    }

    public Account(String id) {
        super(id);
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAccountStatus() {
        return accountStatus;
    }

    public void setAccountStatus(String accountStatus) {
        this.accountStatus = accountStatus;
    }

    public String getResetPassword() {
        return resetPassword;
    }

    public void setResetPassword(String resetPassword) {
        this.resetPassword = resetPassword;
    }

    public Integer getPasswordErrors() {
        return passwordErrors;
    }

    public void setPasswordErrors(Integer passwordErrors) {
        this.passwordErrors = passwordErrors;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Date getPasswordInvalidDate() {
        return passwordInvalidDate;
    }

    public void setPasswordInvalidDate(Date passwordInvalidDate) {
        this.passwordInvalidDate = passwordInvalidDate;
    }

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    public Date getAccountInvalidDate() {
        return accountInvalidDate;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public void setAccountInvalidDate(Date accountInvalidDate) {
        this.accountInvalidDate = accountInvalidDate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * Returns the username used to authenticate the user. Cannot return <code>null</code>.
     *
     * @return the username (never <code>null</code>)
     */
    public String getUsername() {
        return username;
    }

    /**
     * Returns the password used to authenticate the user.
     *
     * @return the password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Indicates whether the user's account has expired. An expired account cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user's account is valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
//        if(getAccountInvalidDate()==null){
//            return false;
//        }
//        return new Date().after(getAccountInvalidDate());
    }

    /**
     * Indicates whether the user is locked or unlocked. A locked user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is not locked, <code>false</code> otherwise
     */
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
//        return StringUtils.equals("account_status_2",getAccountStatus());
    }

    /**
     * Indicates whether the user's credentials (password) has expired. Expired
     * credentials prevent authentication.
     *
     * @return <code>true</code> if the user's credentials are valid (ie non-expired),
     * <code>false</code> if no longer valid (ie expired)
     */
    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
//        return StringUtils.equals("account_status_3",getAccountStatus());
    }

    /**
     * Indicates whether the user is enabled or disabled. A disabled user cannot be
     * authenticated.
     *
     * @return <code>true</code> if the user is enabled, <code>false</code> otherwise
     */
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
//        return StringUtils.equals("Y",getStatus());
    }

    /**
     * Returns the authorities granted to the user. Cannot return <code>null</code>.
     *
     * @return the authorities, sorted by natural key (never <code>null</code>)
     */
    @Override
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return new ArrayList<>();
    }

    /**
     * 账户的简略信息
     * @return
     */
    public Account shortInfo(){
        Account account=new Account(getId());
        account.setUsername(getUsername());
        return account;
    }
}
